﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Question.Core;
using PowerCreator.LMS.Question.Entiry;
using System.Text;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_AddQuestionInfo_zuhe : PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Question.ManagerQuestion  objQuestion = new PowerCreator.LMS.Web.Question.ManagerQuestion();
    Course objCourse = new Course();
    private const string str_Url = "{0}?QuestionID={1}";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.AddQuestion))
        //{
        //    Script.ShowAndHistory("您没有添加试题的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            ViewState["TypeID"] = 7;
            ViewState["UserID"] = 10000;
            ViewState["QuestionID"] = null;
            BinderDLL();

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();

            PreAspNetPager(AspNetPager1);
        } 
    }

    protected override void InitPage()
    {
        lab_AddQuestionInfo_danx_QuestionType.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_AttributeTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_AttributeTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_CourseName.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionState.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionQuestionDiffculty.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionKen.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_Ken", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_DefaultScore.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_DefaultScore", PowerCreator.LMS.Language.LanagetItemType.Page);

        lab_AddQuestionInfo_danx_ContentTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionContent.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Adddanxuan.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_Adddanxuan", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Addduoxuan.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_Addduoxuan", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Addpanduan.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_Addpanduan", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Addjianda.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_AddJianda", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Addtiankong.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_Addtiankong", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Addmingci.Text = GetLanageValueByKey("btn_AddQuestionInfo_zuhe_AddMingcijieshi", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    private string GetSearch()
    {

        StringBuilder search = new StringBuilder();
        
        if (ViewState["QuestionID"] != null)
        {
            string childQuestion = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString())).QuestionItemList;
            if (childQuestion.Trim() != "")
            {
                string[] questionList;
                questionList = childQuestion.Split('$');
                for (int i = 0; i < questionList.Length; i++)
                {
                    search.Append(" and QuestionID=" + questionList[i].ToString());
                }

            }
            else
            {
                search.Append("  1=0 ");
            }

        }
        else
        {
            search.Append("  1=0 ");
        }
        
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objQuestion.CountQuestion(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objQuestion.QueryQuestion(search, LMSPageSize, pageIndex);
        if (dt.Rows.Count > 0)
        {
            pl_AddChildQuestion.Visible = true;
            btnSave.Visible = false;

        }
        else
        {
            pl_AddChildQuestion.Visible = false;
            btnSave.Visible = true;
        }
        dt.Columns.Add("url");
        dt.Columns.Add("Index");
        dt.Columns.Add("QuestionText");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["QuestionContent"] = StringUtils.GetSubstringLue(dt.Rows[i]["QuestionContent"].ToString().Trim(), 30);
            dt.Rows[i]["QuestionText"] = dt.Rows[i]["QuestionAnswer"].ToString().Trim().Replace("$", "") + StringUtils.GetSubstringLue(dt.Rows[i]["QuestionParse"].ToString().Trim(), 10);
            dt.Rows[i]["url"] = dt.Rows[i]["QuestionID"].ToString() + "$" + dt.Rows[i]["FKQuestionTypeID"].ToString(); //试题ID加试题类型ID
            dt.Rows[i]["Index"] = (pageIndex - 1) * 10 + i + 1;
        }
        gvQuestionList.DataSource = dt;
        gvQuestionList.DataBind();
    }

    private void BinderDLL()
    {
        rdbtn_QuestionType.DataSource = objQuestion.QueryQuestionType("");
        rdbtn_QuestionType.DataTextField = "QuestionTypeName";
        rdbtn_QuestionType.DataValueField = "QuestionTypeID";

        rdbtn_QuestionType.DataBind();
        rdbtn_QuestionType.Items[6].Selected = true;
        rdbtn_QuestionType.Items[0].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[1].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[2].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[4].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Mingcijieshiti", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_QuestionType.Items[6].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Zuheti", PowerCreator.LMS.Language.LanagetItemType.Page);


        ddlCourseList.DataSource = objCourse.Query("");
        ddlCourseList.DataTextField = "CourseName";
        ddlCourseList.DataValueField = "Course_Id";
        ddlCourseList.DataBind();
        ddlCourseList.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);

        ddlQuestionState.Items[1].Text = GetLanageValueByKey("dpdlst_ManagerQuestion_IsUse", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionState.Items[0].Text = GetLanageValueByKey("dpdlst_ManagerQuestion_UnUse", PowerCreator.LMS.Language.LanagetItemType.Page);

    }
    private bool PageCheck()
    {
        if (string.IsNullOrEmpty(txtDefaultScore.Text.Trim()) | string.IsNullOrEmpty(txtKen.Text.Trim()) | string.IsNullOrEmpty(txtQuestionContent.Value.Trim()))
        {
            Script.Alert(this, "msg", GetLanageValueByKey("msg_AddQuestionInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (!PageValidate.IsNumber(txtDefaultScore.Text.Trim()))
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_QuestionToIn_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (ddlCourseList.SelectedValue == "-1")
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_QuestionToIn_checkmsg3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }
        VOQuestion voQuestion = new VOQuestion();
        voQuestion.QuestionIsFather = 1;
        voQuestion.FKQuestionCourseID = int.Parse(ddlCourseList.SelectedValue);
        voQuestion.QuestionCreateID = int.Parse(ViewState["UserID"].ToString());
        voQuestion.QuestionCreateTime = DateTime.Now;

        voQuestion.QuestionAnswer = "";            //
        voQuestion.QuestionDefaultScore = int.Parse(txtDefaultScore.Text);
        voQuestion.QuestionDiffculty = int.Parse(ddlDiffculty.SelectedValue);
        voQuestion.QuestionKen = txtKen.Text;


       

        voQuestion.QuestionClass = 3;
        voQuestion.QuestionContent = txtQuestionContent.Value;
        voQuestion.QuestionParse = "";
        voQuestion.FKQuestionTypeID = int.Parse(ViewState["TypeID"].ToString());
        voQuestion.QuestionState = int.Parse(ddlQuestionState.SelectedValue);         //禁用1,启用2
        voQuestion.QuestionItemList = "";
                                                                       
        try
        {
            int questionID=0;
            objQuestion.CreateQuestionOptionList(voQuestion, null, ref questionID);
            pl_AddChildQuestion.Visible = true;
            btnSave.Visible = false;
            ViewState["QuestionID"] = questionID;
            //Script.Alert("操作成功");
            //string url = "EditQuestionInfo_zuhe.aspx?qid=" + ViewState["QuestionID"].ToString();
            string url = "ManagerQuestionInfo.aspx";
            Script.ShowAndRedirect(this, GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page), url);
        }
        catch
        {
            Script.Alert(GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            //Script.ShowAndRedirect(this, "操作失败", "ManagerQuestionInfo.aspx");
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerQuestionInfo.aspx");
    }

    protected void rdbtn_QuestionType_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = string.Empty;
        switch (rdbtn_QuestionType.SelectedValue)
        {
            case "1":
                url = "AddQuestionInfo_panduan.aspx";
                break;
            case "2":
                url = "AddQuestionInfo_danx.aspx";
                break;
            case "3":
                url = "AddQuestionInfo_duox.aspx";
                break;
            case "4":
                url = "AddQuestionInfo_tiank.aspx";
                break;
            case "5":
                url = "AddQuestioninfo_mingci.aspx";
                break;
            case "6":
                url = "AddQuestionInfo_jianda.aspx";
                break;
            case "7":
                url = "AddQuestionInfo_zuhe.aspx";
                break;
        }
        Script.RedirectURL(url);
    }
    protected void gvQuestionList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string str = e.CommandArgument.ToString();
        int index = str.IndexOf("$");
        string questionID = str.Substring(0, index).Replace("$", "");

        string questionTypeID = str.Remove(0, index).Replace("$", "");
        if (e.CommandName == "mydelete")
        {
            int qID = int.Parse(questionID);
            //删除前检查是否有试卷引用
            //if (!CheckQuestion_Del(qID))
            //{
            //    return;
            //}

            #region 删除题目表相关记录
            DataTable dt = new DataTable();
            dt = objQuestion.QueryQuestionOption("FKQuestionOptionQuestionID =" + qID);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int OptionID = int.Parse(dt.Rows[i]["QuestionOptionID"].ToString());
                objQuestion.DeleteQuestionOption(OptionID);
            }
            #endregion

            objQuestion.DeleteQuestion(qID);
            //string url = Utils.WebRoot + "/Message.aspx";
            //Script.OpenURL(url, "toolbar=no,location=no,fullscreen=no,personalbar=no,status=no,menubar=no,scrollbars=no,titlebar=no,resizeable=no,left=500,top=300,width=230,height=120");
            //Script.Alert(this,"msg",msg);
            //Script.ShowMsgRedirectUrl(msg, "ManagerQuestionInfo.aspx");

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        if (e.CommandName == "myeditor")
        {
            string url = String.Empty;
            switch (questionTypeID)
            {
                case "1":
                    url = "EditQuestionInfo_panduan.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
                case "2":
                    url = "EditQuestionInfo_danxuan.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
                case "3":
                    url = "EditQuestionInfo_duoxuan.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
                case "4":
                    url = "EditQuestionInfo_tiank.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
                case "5":
                    url = "EditQuestionInfo_jianda.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
                default:
                    url = "EditQuestionInfo_jianda.aspx?qid=" + Base64.Encode(questionID) + "&TypeID=" + Base64.Encode(questionTypeID); ;
                    break;
            }

            Script.RedirectURL(url);
        }
    }
    protected void gvQuestionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Text = GetLanageValueByKey("Msg_AddQuestionInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[3].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[5].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[7].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //gvQuestionList.Columns[2].Visible = false;

            ImageButton ibtnDelete = (ImageButton)e.Row.Cells[9].FindControl("ibtnDelete");
            ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("Msg_ManagerQuestion_DeletMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
            switch (e.Row.Cells[5].Text.Trim())
            {
                case "1": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "2": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "3": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "4": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "5": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Mingcijieshiti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "6": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
            }

        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Adddanxuan_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_danx.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);
    }
    protected void btn_Addpanduan_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_pand.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);
    }
    protected void btn_Addtiankong_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_tiank.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);

    }
    protected void btn_Addmingci_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_mingci.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);

    }
    protected void btn_Addjianda_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_jiand.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);

    }
    protected void btn_Addduoxuan_Click(object sender, EventArgs e)
    {
        string url = string.Format(str_Url, "CombinationQuestion_Add_duox.aspx", ViewState["QuestionID"].ToString());
        Script.OpenURL(url);
    }
}
